/*
* $Id: msg.c,v 1.15 2007/10/25 02:45:26 chunchia Exp $
*/
/*******************************************************************************

       UBEC (Uniband Electronic Corp.)

       Project: U-NET01, Ubiquitous network platform

       File: msg.c

       Version: 0.3.0

       Usage: Message operation function

       Platform: U-NET01 DK with Keil 8051 C compiler

       Reference:

               Silicon Laboratories: C8051F124, C8051F340

               UBEC: UZ2400, UZ2410

       Note :

               Copyright (C) 2007 Uniband Electronic Corporation, All rights reserved

********************************************************************************/

#include "unet.h"

extern UNET_SYS UnetSys;


/*******************************************************************************

			Main Memoey Function

********************************************************************************/


void *MM_Alloc(UINT8 size){
	void *Addr;

//	DisInt();

	Addr = malloc(size);

//	EnabInt();

	return Addr;
}


void MM_Free(void *allocaddr){

//	DisInt();

	free(allocaddr);

//	EnabInt();
}


void MM_Init(MSG_QUEUE *Queue){

	UINT8 i;

	Queue->QueueDataCount = 0;
	Queue->QueueReadPoint = 0;
	Queue->QueueWritePoint = 0;

	for(i=0;i<MSG_MAX_QUEUE_SIZE; i++){
		if(Queue->Msg[i].DataFlag)
			MM_Free(Queue->Msg[i].MsgPtr);

		Queue->Msg[i].DataFlag = false;
		Queue->Msg[i].MsgPtr = NULL;
	}

}

INT8 MSG_Add(void *Msg, UINT8 msgType, MSG_QUEUE *QueuePtr, UINT8 Length) {
	UINT8 WritePtr;

//	if(QueuePtr->QueueDataCount > MSG_MAX_QUEUE_SIZE)
//		QueuePtr->QueueDataCount = 0x0;

	if(QueuePtr->QueueDataCount < MSG_MAX_QUEUE_SIZE){ // MSG Queue is not full

		WritePtr = QueuePtr->QueueWritePoint; // Fetch empty entry

		do{ // Move to next empty entry
			if(WritePtr == MSG_MAX_QUEUE_SIZE -1){
				QueuePtr->QueueWritePoint = 0;
			}else{
				QueuePtr->QueueWritePoint++;
			}
		}while(QueuePtr->Msg[WritePtr].DataFlag != false);

		// Fill Data
		QueuePtr->Msg[WritePtr].DataFlag = true;
		QueuePtr->Msg[WritePtr].MsgType = msgType;
		QueuePtr->Msg[WritePtr].MsgPtr = Msg;
		QueuePtr->Msg[WritePtr].Length = Length;

		QueuePtr->QueueDataCount ++; // Increase Queue Data Count

		// fill data successful, return message
		return SUCCESS; // Successful
	}

	return QUEUE_FULL; // MSG Queue is full, return message

}
/*
#if defined(__C51__)
#pragma AREGS
#elif defined(__K0__)
	// Nothing special in my domain.
#else
        #error "Unknown Compiler."
#endif
*/
INT8 MSG_Fetch(MSG_QUEUE *QueuePtr){
	UINT8 ReadPtr;

//	if(QueuePtr->QueueDataCount > MSG_MAX_QUEUE_SIZE)
//		QueuePtr->QueueDataCount = 0x0;

	//  Have data in queue? or still need handle?
	if(QueuePtr->QueueDataCount >0){

		// Fetch previous read entry
		while(1){

			ReadPtr = QueuePtr->QueueReadPoint;

			if(QueuePtr->QueueReadPoint == MSG_MAX_QUEUE_SIZE -1){
				QueuePtr->QueueReadPoint = 0;
			}else{
				QueuePtr->QueueReadPoint++;
			}

			if(QueuePtr->Msg[ReadPtr].DataFlag == true){
				return ReadPtr;
			}
		}
	}

	return QUEUE_EMPTY;
}
